package com.haohan.stock.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.util.JdkIdGenerator;

import com.haohan.stock.domain.Operatate;
import com.haohan.stock.domain.StockData;

public class CalcModel {
	/**
	 * 1,积累数据 2,日均线选股 3,板块与板块下个股数据导入 4,单一板块的算法研发 5,基本面分析
	 */
	/**
	 * 算法实现描述:
	 * 
	 */
	/**
	 * 买入点 ?个周期4线下到4线上
	 * 
	 * @param stockData
	 * @return
	 */
	public static boolean dailyAverageStock_buy(StockData stockData) {
		if (stockData.getClose() > stockData.getPrice5() && stockData.getClose() > stockData.getPrice10()
				&& stockData.getClose() > stockData.getPrice20() && stockData.getClose() > stockData.getPrice30()) {
			return true;
		}
		return false;
	}

	/**
	 * 加仓点
	 * 
	 * @param stockData
	 * @return
	 */
	public static boolean dailyAverageStock_buy_add(StockData stockData, int period) {
		if (stockData.getClose() > stockData.getPrice5() && stockData.getClose() > stockData.getPrice10()
				&& stockData.getClose() > stockData.getPrice20() && stockData.getClose() > stockData.getPrice30()) {
			return true;
		}
		return false;
	}

	/**
	 * 卖出点
	 * 
	 * @param stockData
	 * @return
	 */
	public static boolean dailyAverageStock_sell(StockData stockData) {
		if (stockData.getClose() < stockData.getPrice5() && stockData.getClose() < stockData.getPrice10()
				&& stockData.getClose() < stockData.getPrice20() && stockData.getClose() < stockData.getPrice30()) {
			return true;
		}
		return false;
	}

	/**
	 * 减仓点
	 * 
	 * @param stockData
	 * @return
	 */
	public static boolean dailyAverageStock_sell_sub(StockData stockData, int period) {
		if (stockData.getClose() < stockData.getPrice5() && stockData.getClose() < stockData.getPrice10()
				&& stockData.getClose() < stockData.getPrice20() && stockData.getClose() < stockData.getPrice30()) {
			return true;
		}
		return false;
	}

	/**
	 * 个股回测 先实现一个固定的个股一段时间模型，
	 * 
	 * @param stockData
	 */
	public static void backTest(List<StockData> stockDatas, double money, double targetProfit, double stopLoss) {
		String parent_id = new JdkIdGenerator().generateId().toString();
		double sub_money = money;
		double hold_number = 0;// 持有数量
		double hold_money = 0;// 持有市值
		List<Operatate> list = new ArrayList<Operatate>();
		int i = 0;
		for (Iterator<StockData> iterator = stockDatas.iterator(); iterator.hasNext();) {
			StockData stockData = (StockData) iterator.next();
			if (stockData.isBuy_point_second()) {
				Operatate op = new Operatate();
				op.setParent_id(parent_id);
				op.setOperation("买入");
				op.setNumber(calc(20000, stockData.getClose()));
				op.setMoney(op.getNumber() * stockData.getClose());
				hold_number = hold_number+op.getNumber();
				hold_money = hold_money+op.getMoney();
				op.setHold_number(hold_number);
				op.setHold_money(hold_money);
				sub_money = sub_money - hold_money;
				op.setOrder(++i);
				list.add(op);
			}
			if (stockData.isSell_point_second()) {
				Operatate op = new Operatate();
				op.setParent_id(parent_id);
				op.setOperation("卖出");
				op.setNumber(calc(20000, stockData.getClose()));
				op.setMoney(op.getNumber() * stockData.getClose());
				hold_number = hold_number - op.getNumber();
				hold_money = hold_money-op.getMoney();
				op.setHold_number(hold_number);
				op.setHold_money(hold_money);
				sub_money = sub_money + hold_money;
				op.setOrder(++i);
				list.add(op);
			}
		}
	}

	private static Double calc(double money, double price) {
		Double num = money / (price * 100);
		return num.intValue() + 1d;
	}

	/**
	 * 组合回测
	 * 
	 * @param stockData
	 */
	public static void backTestGroup(Map<String, List<StockData>> stockDatas) {

	}
}
